//
//  SelectPayWayView.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/15.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "SelectPayWayView.h"

@interface SelectPayWayView()

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIView *dismissView;

@property (nonatomic,strong) UIButton *preBtn;

@end

@implementation SelectPayWayView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {
        
        _dismissView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 225)];
        _dismissView.userInteractionEnabled = YES;
        [_dismissView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]];
        _dismissView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-225, SCREEN_WIDTH, 225)];
        _bgView.backgroundColor = [UIColor whiteColor];
        
        for (int i = 0; i<2; i++) {
            
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 7+50*i, 36, 36)];
            imgView.image = [UIImage imageNamed:@[@"timg",@"timdg"][i]];
            [_bgView addSubview:imgView];
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 40, 20 + i*50, 10, 10)];
            [btn setImage:[UIImage imageNamed:@"rect"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"ok2"] forState:UIControlStateSelected];

            btn.tag = 200+i;
            [btn addTarget:self action:@selector(selectEvent:) forControlEvents:UIControlEventTouchUpInside];
            [_bgView addSubview:btn];
            
            CALayer *layer = [CALayer layer];
            layer.frame = CGRectMake(0, i*50 + 50, SCREEN_WIDTH, .5);
            layer.backgroundColor = getColor(@"f1f2f6").CGColor;
            [_bgView.layer addSublayer:layer];
        }
        
        UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 24)];
        submitBtn.layer.masksToBounds = YES;
        submitBtn.layer.cornerRadius = 12;
        submitBtn.backgroundColor = getColor(mainColor);
        [submitBtn setTitle:@"去付款" forState:UIControlStateNormal];
        submitBtn.titleLabel.font = DEF_FontSize_12;
        submitBtn.center = CGPointMake(SCREEN_WIDTH/2., 200);
        [_bgView addSubview:submitBtn];
        [self addSubview:_bgView];
        [self addSubview:_dismissView];
        
    }
    return self;
}

- (void)selectEvent:(UIButton *)sender{
    if (sender != self.preBtn) {
        self.preBtn.selected = NO;
        sender.selected = YES;
        self.preBtn = sender;
        
        if (self.typeSelectHandle) {
            self.typeSelectHandle(sender.tag - 200);
        }
    }
}

- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
